<?php

/**
* @copyright   (C) 2010 iJoomla, Inc. - All rights reserved.
* @license  GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html) 
* @author  iJoomla.com webmaster@ijoomla.com
* @url   http://www.ijoomla.com/licensing/
* the PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript  
* are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0 
* More info at http://www.ijoomla.com/licensing/
*/
 
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');
jimport('joomla.utilities.date');

class iJoomla_SurveysModelResponses extends JModelList{
	
	protected $_context = 'com_surveys.responses';
	private $total=0;

	function populateState(){
		// Initialize variables.
		$app = JFactory::getApplication('administrator');
		// Load the list state.
		$this->setState('list.start', $app->getUserStateFromRequest($this->_context . '.list.start', 'limitstart', 0, 'int'));
		$this->setState('list.limit', $app->getUserStateFromRequest($this->_context . '.list.limit', 'limit', $app->getCfg('list_limit', 25) , 'int'));
		$this->setState('selected', JRequest::getVar('cid', array()));		
	}
	
	function getPagination(){
		$pagination=parent::getPagination();
		$pagination->total=$this->total;
		if($pagination->total%$pagination->limit>0){
			$nr_pages=intval($pagination->total/$pagination->limit)+1;
		}	
		else{
		 	$nr_pages=intval($pagination->total/$pagination->limit);
		}	
		$pagination->set('pages.total',$nr_pages);
		$pagination->set('pages.stop',$nr_pages);
		return $pagination;
	}
	
	function getItems(){
		$config = new JConfig();	
		$app = JFactory::getApplication('administrator');
		$limistart = $app->getUserStateFromRequest($this->context.'.list.start', 'limitstart');
		$limit = $app->getUserStateFromRequest($this->context.'.list.limit', 'limit', $config->list_limit);

		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
		$query = $this->getListQuery();
		
		$db->setQuery($query);
		$db->query();
		$result	= $db->loadObjectList();
		if($result != NULL && is_array($result) && count($result)>0){
			$this->total=count($result);
		}
		else{
			$this->total=0;
		}	
		
		$db->setQuery($query,$limistart,$limit);
		$db->query();
		$result	= $db->loadObjectList();
		
		return $result;
	}
	
	function getListQuery(){		
		$database	= JFactory::getDBO();
		$query		= $database->getQuery(true);
		$app 		= JFactory::getApplication('administrator');

		$filter_search = $app->getUserStateFromRequest($this->context.'.filter.search', 'search','','string');
		$this->setState('filter.search', $filter_search, 'string');
		$_id = JRequest::getVar("id", "0");
		$where = " 1=1 and survey_id=".$_id;
		
		if($filter_search != ""){
			$filter_field = $app->getUserStateFromRequest($this->context.'.filter.field', 'field','','string');
			$this->setState('filter.field', $filter_field, 'string');
			
			if($filter_field == "name"){
				$where .= " and u.name like '%".addslashes($filter_search)."%' ";
			}
			elseif($filter_field == "username"){
				$where .= " and u.username like '%".addslashes($filter_search)."%' ";
			}
			elseif($filter_field == "email"){
				$where .= " and u.email like '%".addslashes($filter_search)."%' ";
			}
			elseif($filter_field == "ip"){
				$where .= " and s.ip like '%".addslashes($filter_search)."%' ";
			}
		}
		
		$query = $database->getQuery(true);
		$query->clear();
		$query->select('s.*, u.name, u.username, u.email');
		$query->from('#__ijoomla_surveys_session s');
		$query->join('LEFT', '`#__users` AS u ON u.id = s.user_id');
		$query->where($where);
		$query->order("played_time asc");
								
		return $query;		
	}
	
	function publish_unpublish(){
		$task = JRequest::getVar("task", "");
		$value = "";
		$ids = JRequest::getVar("cid");
		if($task == "publish"){
			$value = "1";
		}
		else{
			$value = "0";
		}
		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->clear();
		foreach($ids as $key=>$id){
			$query->clear();
			$query->update("#__ijoomla_surveys_session");
			$query->set("published=".$value);
			$query->where("id=".$id);
			$db->setQuery($query);
			if(!$db->query()){				
				return false;
			}
		}		
		return true;
	}
	
	function clear(){
		$ids = JRequest::getVar("cid");
		$db =& JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->clear();
		foreach($ids as $key=>$id){
			$query->clear();
			$query->delete("#__ijoomla_surveys_session");
			$query->where("id=".$id);
			$db->setQuery($query);
			if($db->query()){				
				$query->clear();
				$query->delete("#__ijoomla_surveys_result");
				$query->where("session_id=".$id);
				$db->setQuery($query);	
				$db->query();
				
				$query->clear();
				$query->delete("#__ijoomla_surveys_result_text");
				$query->where("session_id=".$id);
				$db->setQuery($query);	
				$db->query();		
			}
			else{
				return false;
			}
		}
		return true;
	}
	
}

?>